
import struct
from packet import Packet

MAC_ADDR_SIZE = 6
ETHERNET_TYPE_IP = 0x0800
ETHERNET_TYPE_ARP = 0x0806

ETHERNET_BROADCAST = "\xff" * MAC_ADDR_SIZE

class Ethernet(Packet):
    def __init__(self, src_mac, dst_mac, protocol, data):
        Packet.__init__(self)
        
        # check packet arguments
        if (MAC_ADDR_SIZE != len(src_mac)) or (MAC_ADDR_SIZE != len(dst_mac)):
            raise Exception("Invalid MAC address length")
         
        # set local members
        self.m_src_mac = src_mac
        self.m_dst_mac = dst_mac
        self.m_data = data
        
        # build headers
        self.m_header = self.m_dst_mac + self.m_src_mac + struct.pack("!H", protocol)
        
    @staticmethod
    def mac_addr_ntoa(mac_addr_net):
        return ":".join(map(lambda x: "%02X" % ord(x), mac_addr_net))
    
    @staticmethod
    def mac_addr_aton(mac_addr_str):
        return "".join(map(lambda x: chr(long(x, 16)), mac_addr_str.split(":")))